package com.global.gbinventory.barang.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JComboBox;

public class BarangFrame extends JInternalFrame {

	private static final long serialVersionUID = -8987236922544778928L;

	private JPanel panelButtons;
	private JPanel panelInfo;
	
	private JTextField txtFldKode;
	private JTextField txtFldNama;
	private JTextField txtFldGrup;
	private JTextField txtFldSubgrup;
	private JTextField txtFldMerek;
	private JTextField txtFldBarcode;
	private JTextField txtFldMinimumStok;
	
	private JTextArea txtAreaInfoProduk;
	
	private JComboBox cmbBoxSatuan;
	private JComboBox cmbBoxGudang;
	
	private JLabel lblKode;
	private JLabel lblNama;
	private JLabel lblGambar;
	
	private JButton btnNew;
	private JButton btnEdit;
	private JButton btnDelete;
	private JButton btnSave;
	private JButton btnCancel;
	private JButton btnShowKodeList;
	private JButton btnPilihGambar;
	private JButton btnShowGrupList;
	private JButton btnShowMerekList;
	
	private JFileChooser fc;
	private JPanel panelBlankGambarLeft;
	private JPanel panelBlankGambarTop;
	private JPanel panelBlankGambarRight;
	private JPanel panelBlankGambarBottom;
	
	
	/**
	 * Create the frame.
	 */
	public BarangFrame() {
		//resizeable, closeable, maximizable, iconifiable
		super("Data Produk", false, true, false, true);
		setBounds(100, 100, 870, 473);
		getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panelRight = new JPanel();
		panelRight.setPreferredSize(new Dimension(20, 10));
		getContentPane().add(panelRight, BorderLayout.EAST);

		JPanel panelCenter = new JPanel();
		getContentPane().add(panelCenter, BorderLayout.CENTER);
		panelCenter.setLayout(null);

		panelButtons = new JPanel();
		panelButtons.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelButtons.setBounds(20, 11, 800, 40);
		panelCenter.add(panelButtons);
		panelButtons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		btnNew = new JButton("New");
		panelButtons.add(btnNew);

		btnEdit = new JButton("Edit");
		btnEdit.setEnabled(false);
		panelButtons.add(btnEdit);

		btnDelete = new JButton("Delete");
		btnDelete.setEnabled(false);
		panelButtons.add(btnDelete);

		btnSave = new JButton("Save");
		btnSave.setEnabled(false);
		panelButtons.add(btnSave);

		btnCancel = new JButton("Cancel");
		btnCancel.setEnabled(false);
		panelButtons.add(btnCancel);

		panelInfo = new JPanel();
		panelInfo.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelInfo.setBounds(20, 63, 800, 316);
		panelCenter.add(panelInfo);
		panelInfo.setLayout(null);

		btnShowKodeList = new JButton("...");
		btnShowKodeList.setBounds(273, 42, 24, 23);
		panelInfo.add(btnShowKodeList);

		JLabel lblInfoUmum = new JLabel("Info Umum");
		lblInfoUmum.setFont(new Font("Arial", Font.PLAIN, 11));
		lblInfoUmum.setOpaque(true);
		lblInfoUmum.setBackground(new Color(102, 153, 204));
		lblInfoUmum.setBounds(10, 11, 585, 14);
		panelInfo.add(lblInfoUmum);

		lblKode = new JLabel("Kode* : ");
		lblKode.setFont(new Font("Arial", Font.PLAIN, 11));
		lblKode.setBounds(10, 46, 71, 14);
		panelInfo.add(lblKode);

		txtFldKode = new JTextField();
		txtFldKode.setEditable(false);
		txtFldKode.setBounds(93, 42, 173, 20);
		panelInfo.add(txtFldKode);
		txtFldKode.setColumns(20);

		lblNama = new JLabel("Nama* : ");
		lblNama.setFont(new Font("Arial", Font.PLAIN, 11));
		lblNama.setBounds(10, 78, 71, 14);
		panelInfo.add(lblNama);

		txtFldNama = new JTextField();
		txtFldNama.setEditable(false);
		txtFldNama.setBounds(93, 74, 204, 20);
		panelInfo.add(txtFldNama);
		txtFldNama.setColumns(40);

		JLabel lblGrup = new JLabel("Grup :");
		lblGrup.setFont(new Font("Arial", Font.PLAIN, 11));
		lblGrup.setBounds(10, 110, 71, 14);
		panelInfo.add(lblGrup);

		JLabel lblSubgrup = new JLabel("Sub-Grup :");
		lblSubgrup.setFont(new Font("Arial", Font.PLAIN, 11));
		lblSubgrup.setBounds(10, 142, 71, 14);
		panelInfo.add(lblSubgrup);

		txtFldGrup = new JTextField();
		txtFldGrup.setEditable(false);
		txtFldGrup.setColumns(20);
		txtFldGrup.setBounds(93, 106, 173, 20);
		panelInfo.add(txtFldGrup);

		txtFldSubgrup = new JTextField();
		txtFldSubgrup.setEditable(false);
		txtFldSubgrup.setColumns(50);
		txtFldSubgrup.setBounds(93, 138, 204, 20);
		panelInfo.add(txtFldSubgrup);

		JLabel lblInfoGambar = new JLabel("Gambar");
		lblInfoGambar.setFont(new Font("Arial", Font.PLAIN, 11));
		lblInfoGambar.setBackground(new Color(102, 153, 204));
		lblInfoGambar.setOpaque(true);
		lblInfoGambar.setBounds(615, 11, 169, 14);
		panelInfo.add(lblInfoGambar);

		JLabel lblMerek = new JLabel("Merek : ");
		lblMerek.setFont(new Font("Arial", Font.PLAIN, 11));
		lblMerek.setBounds(323, 46, 65, 14);
		panelInfo.add(lblMerek);

		txtFldMerek = new JTextField();
		txtFldMerek.setFont(new Font("Arial", Font.PLAIN, 11));
		txtFldMerek.setEditable(false);
		txtFldMerek.setBounds(390, 43, 173, 20);
		panelInfo.add(txtFldMerek);
		txtFldMerek.setColumns(30);

		JLabel lblGudang = new JLabel("Gudang :");
		lblGudang.setFont(new Font("Arial", Font.PLAIN, 11));
		lblGudang.setBounds(323, 110, 65, 14);
		panelInfo.add(lblGudang);

		txtFldBarcode = new JTextField();
		txtFldBarcode.setFont(new Font("Arial", Font.PLAIN, 11));
		txtFldBarcode.setEditable(false);
		txtFldBarcode.setBounds(390, 139, 205, 20);
		panelInfo.add(txtFldBarcode);
		txtFldBarcode.setColumns(10);

		JLabel lblSatuan = new JLabel("Satuan : ");
		lblSatuan.setFont(new Font("Arial", Font.PLAIN, 11));
		lblSatuan.setBounds(323, 78, 60, 14);
		panelInfo.add(lblSatuan);

		JLabel lblBarcode = new JLabel("Barcode :");
		lblBarcode.setFont(new Font("Arial", Font.PLAIN, 11));
		lblBarcode.setBounds(323, 142, 60, 14);
		panelInfo.add(lblBarcode);
		
		JLabel lblInfoProduk = new JLabel("Info Produk");
		lblInfoProduk.setOpaque(true);
		lblInfoProduk.setFont(new Font("Arial", Font.PLAIN, 11));
		lblInfoProduk.setBackground(new Color(102, 153, 204));
		lblInfoProduk.setBounds(10, 174, 288, 14);
		panelInfo.add(lblInfoProduk);
		
		JLabel lblMinimum = new JLabel("Minimum :");
		lblMinimum.setFont(new Font("Arial", Font.PLAIN, 11));
		lblMinimum.setBounds(323, 204, 71, 14);
		panelInfo.add(lblMinimum);
		
		txtFldMinimumStok = new JTextField();
		txtFldMinimumStok.setEditable(false);
		txtFldMinimumStok.setColumns(50);
		txtFldMinimumStok.setBounds(400, 200, 195, 20);
		panelInfo.add(txtFldMinimumStok);
		
		JLabel lblInfoStok = new JLabel("Info Stok");
		lblInfoStok.setOpaque(true);
		lblInfoStok.setFont(new Font("Arial", Font.PLAIN, 11));
		lblInfoStok.setBackground(new Color(102, 153, 204));
		lblInfoStok.setBounds(323, 172, 272, 14);
		panelInfo.add(lblInfoStok);
		
		txtAreaInfoProduk = new JTextArea();
		txtAreaInfoProduk.setBounds(10, 200, 287, 101);
		txtAreaInfoProduk.setEditable(false);
		panelInfo.add(txtAreaInfoProduk);
		
		JPanel panel = new JPanel();
		panel.setBounds(615, 249, 169, 35);
		panelInfo.add(panel);
		
		btnPilihGambar = new JButton("Pilih Gambar");
		btnPilihGambar.setEnabled(false);
		panel.add(btnPilihGambar);
		
		cmbBoxSatuan = new JComboBox();
		cmbBoxSatuan.setBounds(390, 72, 205, 27);
		cmbBoxSatuan.setEditable(false);
		cmbBoxSatuan.setEnabled(false);
		panelInfo.add(cmbBoxSatuan);
		
		cmbBoxGudang = new JComboBox();
		cmbBoxGudang.setBounds(390, 104, 205, 27);
		cmbBoxGudang.setEditable(false);
		cmbBoxGudang.setEnabled(false);
		panelInfo.add(cmbBoxGudang);
		
		btnShowGrupList = new JButton("...");
		btnShowGrupList.setBounds(273, 103, 24, 23);
		btnShowGrupList.setEnabled(false);
		panelInfo.add(btnShowGrupList);
		
		btnShowMerekList = new JButton("...");
		btnShowMerekList.setBounds(571, 39, 24, 23);
		btnShowMerekList.setEnabled(false);
		panelInfo.add(btnShowMerekList);
		
		JPanel panelGambar = new JPanel();
		panelGambar.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panelGambar.setBounds(615, 37, 169, 200);
		panelInfo.add(panelGambar);
		panelGambar.setLayout(new BorderLayout(0, 0));
		
		lblGambar = new JLabel("No Picture");
		lblGambar.setHorizontalAlignment(SwingConstants.CENTER);
		panelGambar.add(lblGambar, BorderLayout.CENTER);
		
		panelBlankGambarLeft = new JPanel();
		panelBlankGambarLeft.setPreferredSize(new Dimension(8, 5));
		panelGambar.add(panelBlankGambarLeft, BorderLayout.WEST);
		
		panelBlankGambarTop = new JPanel();
		panelBlankGambarTop.setPreferredSize(new Dimension(10, 5));
		panelGambar.add(panelBlankGambarTop, BorderLayout.NORTH);
		
		panelBlankGambarRight = new JPanel();
		panelBlankGambarRight.setPreferredSize(new Dimension(5, 10));
		panelGambar.add(panelBlankGambarRight, BorderLayout.EAST);
		
		panelBlankGambarBottom = new JPanel();
		panelBlankGambarBottom.setPreferredSize(new Dimension(10, 5));
		panelGambar.add(panelBlankGambarBottom, BorderLayout.SOUTH);

		JPanel panelNB = new JPanel();
		FlowLayout fl_panelNB = (FlowLayout) panelNB.getLayout();
		fl_panelNB.setAlignment(FlowLayout.LEFT);
		panelNB.setBounds(20, 390, 800, 25);
		panelCenter.add(panelNB);

		JLabel lblNb = new JLabel("NB : * Harus diisi");
		panelNB.add(lblNb);
		
	}
	
	public void displayFrame(boolean state){
		setVisible(state);
	}
	
	public void addBtnNewListener(ActionListener l){
		btnNew.addActionListener(l);
	}
	
	public void addBtnEditListener(ActionListener l){
		btnEdit.addActionListener(l);
	}
	
	public void addBtnDeleteListener(ActionListener l){
		btnDelete.addActionListener(l);
	}
	
	public void addBtnSaveListener(ActionListener l){
		btnSave.addActionListener(l);
	}
	
	public void addBtnCancelListener(ActionListener l){
		btnCancel.addActionListener(l);
	}
	
	public void addBtnShowListListener(ActionListener l){
		btnShowKodeList.addActionListener(l);
	}
	
	public void addBtnShowGrupListListener(ActionListener l){
		btnShowGrupList.addActionListener(l);
	}
	
	public void addBtnShowMerekListListener(ActionListener l){
		btnShowMerekList.addActionListener(l);
	}
	
	public void addBtnPilihGambarListener(ActionListener l){
		btnPilihGambar.addActionListener(l);
	}
	
	public JFileChooser getPilihGambarFC(){
		if(fc == null){
			fc = new JFileChooser();
		}
		
		return fc;
	}
	
	public void requestFocusTxtFldKode(){
		txtFldKode.requestFocusInWindow();
	}
	
	public void setEnabledBtnNew(boolean state){
		btnNew.setEnabled(state);
	}
	
	public void setEnabledBtnEdit(boolean state){
		btnEdit.setEnabled(state);
	}
	
	public void setEnabledBtnDelete(boolean state){
		btnDelete.setEnabled(state);
	}
	
	public void setEnabledBtnSave(boolean state){
		btnSave.setEnabled(state);
	}
	
	public void setEnabledBtnCancel(boolean state){
		btnCancel.setEnabled(state);
	}
	
	public void setEnabledBtnShowList(boolean state){
		btnShowKodeList.setEnabled(state);
	}
	
	public void setEnabledBtnShowGrupList(boolean state){
		btnShowGrupList.setEnabled(state);
	}
	
	public void setEnabledBtnShowMerekList(boolean state){
		btnShowMerekList.setEnabled(state);
	}
	
	public void setCmbBoxSatuanModel(DefaultComboBoxModel model){
		cmbBoxSatuan.setModel(model);
	}
	
	public void setCmbBoxGudangModel(DefaultComboBoxModel model){
		cmbBoxGudang.setModel(model);
	}
	
	public void setCmbBoxSatuanSelectedIndex(int index){
		cmbBoxSatuan.setSelectedIndex(index);
	}
	
	public void setCmbBoxGudangSelectedIndex(int index){
		cmbBoxGudang.setSelectedIndex(index);
	}
	
	public String getKode() {
		return txtFldKode.getText();
	}
	
	public void setKode(String text){
		txtFldKode.setText(text);
	}

	public String getNama() {
		return txtFldNama.getText();
	}

	public void setNama(String text){
		txtFldNama.setText(text);
	}
	
	public String getGrup() {
		return txtFldGrup.getText();
	}
	
	public void setGrup(String text){
		txtFldGrup.setText(text);
	}

	public String getSubgrup() {
		return txtFldSubgrup.getText();
	}
	
	public void setSubgrup(String text){
		txtFldSubgrup.setText(text);
	}

	public String getMerek() {
		return txtFldMerek.getText();
	}
	
	public void setMerek(String text){
		txtFldMerek.setText(text);
	}

	public String getBarcode() {
		return txtFldBarcode.getText();
	}

	public void setBarcode(String text){
		txtFldBarcode.setText(text);
	}
	
	public String getSatuan() {
		return cmbBoxSatuan.getSelectedItem().toString();
	}
	
	public void setSatuan(Object text){
		cmbBoxSatuan.setSelectedItem(text);
	}

	public String getGudang() {
		return cmbBoxGudang.getSelectedItem().toString();
	}
	
	public void setGudang(Object text){
		cmbBoxGudang.setSelectedItem(text);
	}

	public String getInfoProduk() {
		return txtAreaInfoProduk.getText();
	}
	
	public void setInfoProduk(String text){
		txtAreaInfoProduk.setText(text);
	}

	public String getMinimumStok() {
		return txtFldMinimumStok.getText();
	}
	
	public void setMinimumStok(String text){
		txtFldMinimumStok.setText(text);
	}
	
	public void setGambar(ImageIcon icon){
		lblGambar.setIcon(icon);
	}
	
	public void setTextFieldEditable(){
		txtFldKode.setEditable(true);
		txtFldNama.setEditable(true);
		txtFldBarcode.setEditable(true);
		txtFldMerek.setEnabled(true);
		cmbBoxGudang.setEnabled(true);
		cmbBoxSatuan.setEnabled(true);
		txtFldMinimumStok.setEditable(true);
		txtAreaInfoProduk.setEditable(true);
		btnPilihGambar.setEnabled(true);
	}
	
	public void setTextFieldNonEditable(){
		txtFldKode.setEditable(false);
		txtFldNama.setEditable(false);
		txtFldBarcode.setEditable(false);
		txtFldMerek.setEnabled(false);
		cmbBoxGudang.setEnabled(false);
		cmbBoxSatuan.setEnabled(false);
		txtFldMinimumStok.setEditable(false);
		txtAreaInfoProduk.setEditable(false);
		btnPilihGambar.setEnabled(false);
	}
	
	public void clearTextField(){
		txtFldKode.setText("");
		txtFldNama.setText("");
		txtFldGrup.setText("");
		txtFldSubgrup.setText("");
		txtFldBarcode.setText("");
		txtFldMerek.setText("");
//		cmbBoxGudang.setText("");
//		cmbBoxSatuan.setText("");
		txtFldMinimumStok.setText("");
		txtAreaInfoProduk.setText("");
	}
}
